﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QuanLyDoiXe
{
    public partial class frmTiepNhanDiaDiem : Form
    {
        public frmTiepNhanDiaDiem()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ResetControls()
        {
            foreach (Control ctr in grpThongTinDiaDiem.Controls)
            {
                if(ctr is TextBox)
                {
                    ctr.Text = "";
                }
                if(ctr is ComboBox)
                {
                    ((ComboBox)ctr).SelectedIndex = -1;
                }
            }
        }

        private void frmTiepNhanDiaDiem_Load(object sender, EventArgs e)
        {
            try
            {
                cboLoaiDiaDiem.DataSource = LoaiDiaDiemBUS.LayDanhSach();
                cboLoaiDiaDiem.DisplayMember = "TenLoaiDiaDiem";
                cboLoaiDiaDiem.SelectedIndex = -1;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private string KiemTra()
        {
            string strKiemTra = "";
            if(cboLoaiDiaDiem.SelectedIndex == -1)
            {
                strKiemTra += "Hãy chọn loại địa điểm\n";
                return strKiemTra;
            }
            int loaiDiaDiem = cboLoaiDiaDiem.SelectedIndex;
            if(loaiDiaDiem != 1)
            {
                if(txtTenDiaDiem.Text.Trim() == "")
                {
                    strKiemTra += "Xin nhập tên địa điểm\n";
                }
            }
            if(txtNguoiLienHe.Text.Trim() == "")
            {
                strKiemTra += "Xin nhập tên người liên hệ\n";
            }
            if(txtDiaChi.Text.Trim() == "")
            {
                strKiemTra += "Xin nhập địa chỉ\n";
            }
            return strKiemTra;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            string strChuoiLoi = KiemTra();
            if(strChuoiLoi != "")
            {
                MessageBox.Show(strChuoiLoi);
                return;
            }

            DiaDiemDTO diaDiemDto = new DiaDiemDTO();
            diaDiemDto.TenDiaDiem = txtTenDiaDiem.Text.Trim();
            diaDiemDto.LoaiDiaDiem = ((LoaiDiaDiemDTO)cboLoaiDiaDiem.SelectedItem).MaLoaiDiaDiem;
            diaDiemDto.NguoiLienHe = txtNguoiLienHe.Text.Trim();
            diaDiemDto.SoDienThoai = txtSoDienThoai.Text.Trim();
            diaDiemDto.Email = txtEmail.Text.Trim();
            diaDiemDto.ToaDo = txtToaDo.Text.Trim();
            diaDiemDto.DiaChi = txtDiaChi.Text.Trim();
            try
            {
                if(DiaDiemBUS.Them(diaDiemDto) != 0)
                {
                    MessageBox.Show("Đã thêm");
                }
                else
                {
                    MessageBox.Show("Lưu thất bại");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            ResetControls();
        }
    }
}
